﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.Gateway.Configuration.Converter;
using System.Diagnostics;
using Newtonsoft.Json.Converters;

namespace Microsoft.Gateway.Configuration
{
    [Flags]
    public enum ActionType
    {
        Rewrite                 = 0x00,
        PermanentRedirect       = 0x11,
        FoundRedirect           = 0x12,
        SeeOtherRedirect        = 0x14,
        TemporaryRedirect       = 0x18,
        Redirect                = 0x10,
    }

    [Flags]
    public enum TreatmentType
    {
        ContentRewrite          = 0x01,
        LocationRedirect        = 0x02,
        ContentRewriteReturnUrl = 0x04,
        BadCharsFix             = 0x08,

        Passthrough             = 0x00,
        Rewrite                 = ContentRewrite | LocationRedirect | ContentRewriteReturnUrl,
        RewriteAndFixup         = Rewrite | BadCharsFix,
    }

    public class Role 
    {
        // Special Roles
        public const string ROLE_NAME_DEFAULT = "_default";

        public Role()
        {            
            this.RequireAuthenticationBelow = new List<string>();
            this.Flights = new List<Flight>();
            this.Action = DefaultAction();
            this.Treatment = DefaultTreatment();
        }

        [JsonConverter(typeof(StringEnumConverter))]
        public ActionType Action { get; set; }

        public IList<Flight> Flights { get; set; }

        public string Redirect { get; set; }

        public IList<string> RequireAuthenticationBelow { get; set; }
        
        public string Segment { get; set; }
        
        [JsonConverter(typeof(TargetRouteConverter))]
        public IList<TargetRoute> Target { get; set; }
        
        public string TargetBasePath { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]
        public TreatmentType Treatment { get; set; }

        [JsonIgnore]
        public bool IsDefaultRole 
        {
            get { return String.Equals(this.Segment, ROLE_NAME_DEFAULT, StringComparison.OrdinalIgnoreCase); }
        }

        public string GetEffectiveRedirectBase()
        {
            string redirectBase = string.Empty;

            if (IsArrBaseTarget)
            {
                //name of the arr webfarm
                redirectBase = Segment;
            }
            else if (Target != null && Target.Count == 1)
            {
                // it is not an arr flight
                redirectBase = Target.First().Redirect;
            }
            else
            {
                throw new ArgumentException("Role Target config must specify either a single url for redirect or a weighted route collection");
            }

            return redirectBase;
        }

        private ActionType DefaultAction()
        {
            //this could be moved to a config file in the future if desired
            return ActionType.Rewrite;
        }

        private TreatmentType DefaultTreatment()
        {
            //this could be moved to a config file in the future if desired
            return TreatmentType.Rewrite;
        }

        public bool IsArrBaseTarget
        {
            get
            {
                return Target != null && Target.Any() && Target.Count > 1;
            }
        }

    }
}
